package textfield;


import org.w3c.dom.Element;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.Attributes;
import java.util.Enumeration;
import java.util.Hashtable;


/**
 * Question.java
 *
 *
 * Created: Sat Jul 24 10:11:17 2010
 *
 * @author <a href="mailto:fred@theseibels.com">Fred Seibel</a>
 * @version 1.0
 */
public class Question implements Operation {
    // letters contains the list of letters corresponding to the drug list that should be 
    // presented when the answers are generated for this question
    Hashtable letters = new Hashtable();
    
    String question;
    String tag;
    Answers answers = new Answers ();
    String answer = "";
    String generalQuestion = null;
  
    Question (Attributes attributes) {
	tag = attributes.getValue("o");
	question = attributes.getValue("t");
	generalQuestion = attributes.getValue("g");
	// Assumes list is a comma separated list of letters e.g.' R,S,T,U,V
	String list = attributes.getValue("drug");
	if (list != null) {
	    for (int i = 0; i < list.length(); i += 2) {
		String letter = list.substring(i,i+1);
		letters.put(letter, "");
	    }
	}
	Proto.operations.put(tag, this);
    }
    public Operation doOperation(Device device) {	 
	if (letters.size() > 0) {
	    // make answers on the fly because of language change, order changes
	    makeAnswers();
	}
	if (generalQuestion != null &&  Proto.patientAnswers.get(generalQuestion) != null) {
	    // this question has been answered so 
	    Integer choice =  (Integer) (Proto.patientAnswers.get(generalQuestion));
	    Answer answer =  (Answer) answers.getAnswers().elementAt(choice.intValue());
	    String dest = (String) answers.dests.get(answer);
	    Proto.nextOp = (Operation) Proto.operations.get(dest);
	    //return device.writeQuestion(this, choice.intValue());
	    return device.skipOperation();

	} 

	return device.writeQuestion(this, -1);
    }

    String getQuestion() {
	try {
	    return Proto.reconstruct.getLine(Integer.valueOf(question).intValue());
	}
	catch (java.lang.NumberFormatException e) {
	    return question;
	}
    }
    void makeAnswers() {
	answers = new Answers();
	for (int i  = 0; i < Proto.drugs.size(); i++) {
	    Drug drug = (Drug) Proto.drugs.elementAt(i);
	    String firstLetter = Proto.reconstruct.getLine(drug.textNumber).substring(0,1);
	    int country = Integer.parseInt((String) (Proto.recordAnswers.get("x")));
	    int cond = Integer.parseInt(drug.conditional);
	    if (letters.containsKey(firstLetter) && ((country & cond) > 0)) {
		addAnswer(drug.n, "" + drug.textNumber, drug.dest, drug.conditional, null);
	    }
	}
	answers.sortRows();
    }

    void addAnswer(String n, String text, String action, String c, String x) {

	Answer answer = new Answer(Integer.valueOf(n), 
				   text.trim(),(c == null) ? c : c.trim(),
				   (x == null) ? x : x.trim());
	answers.put(answer, action);

    }
    public String toString() {
	String retval = "Question " + tag + ":\n";
	for (Enumeration e =  answers.list.elements(); e.hasMoreElements();) {
	    retval += "" + e.nextElement() +"\n";
	}

	return retval;
    }
} // Question
